jQuery(document).ready(function(){
	jQuery("#email_type").change(function(){
		var input_data = jQuery("#email_type :selected").val();
		jQuery("#email_type option[value='None']").remove();
		jQuery.ajax({
			type: "POST",
			url: "./process/processGetEmails.php",
			data: {"email_type" : input_data},
			success:function(data){
				jQuery("#email").html(data);
				if(jQuery("#email option").length > 0){
					getEmailContent();
				}
			}
		});
	});
	CKEDITOR.replace("output");
	jQuery("#email").change(function(){
		getEmailContent();
	});
	
	jQuery("#create_email_type").click(function(){
		createEmailType();
	});
	
	jQuery("#create_email").click(function(){
		createEmail();
	});
	
	jQuery("#edit_email").click(function(){
		editEmail();
	});
});

var getEmailContent = function(){
	var input_data = jQuery("#email :selected").val();
	var input_data2 = jQuery("#email_type :selected").val();
	var input = "";
	jQuery.ajax({
		type: "POST",
		url: "./process/processGetEmail.php",
		data: {"email" : input_data, "email_type" : input_data2},
		success:function(input){
			var name = jQuery("#name").val();
			if(name != null){
				CKEDITOR.instances["output"].setData(name + ",<br />" + input);
			}else{
				CKEDITOR.instances["output"].setData(input);
			}
		}
	});
}

var createEmailType = function(){
	var new_type = prompt("Please type the name of the new email type: ", "New Type");
	jQuery.ajax({
		type: "POST",
		url: "./process/processNewType.php",
		data: {"new_type" : new_type},
		success:function(input){
			//nothing
		}
	});
}

var createEmail = function(){
	window.location = "./createEmailTemplate.php";
}

var editEmail = function(){
	window.location = "./editEmailTemplate.php";
}